package com.yzh.homework;

import java.io.*;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author 于振晗
 * @title: StudentHomeWork
 * @description: TODO
 * @date 2020年09月08日
 * @path com.yzh.homework
 */
public class StudentHomeWork {
    static boolean flag = true;
    static String path = "d:/sxtjava/student.txt";

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Set<Student> set = new TreeSet<Student>((obj1, obj2) -> {
            if (obj1.getId() == obj2.getId()) {
                return 0;
            }
            return obj1.getAge() - obj2.getAge();
        });
        set.add(new Student(4, "444", 44));
        set.add(new Student(5, "555", 55));
        set.add(new Student(7, "777", 77));
        set.add(new Student(6, "666", 66));
        set.add(new Student(8, "333", 88));
        while (flag) {
            Student student = new Student();
            System.out.println("请输入学生信息");
            System.out.println("请输入学号");
//            String id = inputs(sc.next());
            String id = inputs("exit");
            if (id == null) {
                break;
            } else {
                student.setId(Integer.parseInt(id));
            }
            System.out.println("请输入姓名");
            String name = inputs(sc.next());
            if (name == null) {
                break;
            } else {
                student.setName(name);
            }
            System.out.println("请输入年龄");
            String age = inputs(sc.next());
            if (age == null) {
                break;
            } else {
                student.setAge(Integer.parseInt(age));
            }
            set.add(student);
        }
        set.forEach(item -> {
            System.out.println(item);
            writeTxt(item);
            newLine();
        });
        //存储换行符之后找不到解决方案
        //java.io.StreamCorruptedException: invalid type code: 0D
        getObj();

    }

    public static String inputs(String str) {
        if (str.equals("exit")) {
            flag = false;
            return null;
        }
        return str;
    }

    //追加一个换行符
    public static void newLine() {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(path, true))) {
            bw.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //写入txt文本
    public static void writeTxt(Student student) {
        /**
         判断是否是第一次写入
         */
        File file = new File(path);
        if (file.length() < 1) {
            try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file, true))){
                oos.writeObject(student);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try(MyObjectOutputStream mos = new MyObjectOutputStream(new FileOutputStream(file, true))){
                mos.writeObject(student);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //从文本获取对象（失败）
    public static void getObj() {
        try {
            ObjectInputStream br = new ObjectInputStream(new FileInputStream(path));
            System.out.println(br.readObject());
            System.out.println(br.readObject());
            System.out.println(br.readObject());
            System.out.println(br.readObject());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

class MyObjectOutputStream extends ObjectOutputStream {

    private static boolean flag = true;//判断是否为第一次写入文件
    private static boolean one = true;

    //    public MyObjectOutputStream() throws IOException, SecurityException {
//        // TODO Auto-generated constructor stub
//    }
//
//
//    protected void writeStreamHeader()
//            throws IOException {
//        return; //不写入头部
//    }
    @Override
    public void writeStreamHeader() throws IOException {
        return;
    }

    public MyObjectOutputStream(OutputStream out) throws IOException {
        super(out);
        // TODO Auto-generated constructor stub
    }

}

class Student implements Serializable {
    private static final long serialVersionUID = -1428002280092660932L;
    private int id;
    private String name;
    private int age;

    public Student() {
    }

    public Student(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}